/**
 * 据 BachEditor 弹窗组件构建
 */
'use strict';

function temp(template, data) {
    var str = template || '';
    // Convert the template into string
    $.each(data, function(key, val) {
        var _type = typeof val, re = new RegExp('{{\\s*' + key + '\\s*}}', 'g');
        if (_type === 'object' && val !== null) {
            $.each(val, function(k, v) {
                var r = new RegExp('{{\\s*' + key + '.' + k + '\\s*}}', 'g');
                str = str.replace(r, v);
            });
        } else {
            str = str.replace(re, val);
        }
    });
    return str;
};

function oceModal(option) {
    if (typeof option !== 'object') {
        if (option === 'hide') {
            $('.ocemodal').modal('hide');
            return;
        } else if (option === 'toggle') {
            $('.ocemodal').modal('toggle');
            return;
        } else {
            option = {
                content : option,
                hideDone : true,
            };
        }
    }

    var OPT = $.extend({
        hideTitle : false,
        hideFooter: false,
        url       : null,
        large     : false,
        static	  : true,
        title     : '警告',
        content   : '未知错误',
        $content  : null,
        hideClose : false,
        closeText : '关闭',
        hideDone  : false,
        doneText  : '确定',
        hideAdditional : true,
        additionalText : '其他',
        doneFn : function() {
            $('.ocemodal').modal('hide');
        },
        additionalFn : function() {
            $('.ocemodal').modal('hide');
        },
        cancelFn : function() {
            $('.ocemodal').modal('hide');
        },
        show : function() {
        },
        shown : function(e) {
        },
        hide : function() {
        },
        hidden : function() {
        },
        loaded : function() {
        }
    }, option);

    var dom = '<div class="ocemodal modal" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">'
        + '<div class="modal-dialog '+ (OPT.large ? 'modal-lg' : '') + '">'
        + '<div class="modal-content">'

        + (OPT.hideTitle ? '' : '<div class="modal-header">'
        + '<h4 class="modal-title">{{title}}</h4>'
        + '<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>'
        + '</div>')

        + '<div class="modal-body">'
        + '<p class="oceModal-content">'
        + '</div>'

        + (OPT.hideFooter ? '' : '<div class="modal-footer">'
        + (OPT.hideClose ? '' : '<button type="button" class="btn btn-raised btn-default cancel-btn" data-dismiss="modal">{{closeText}}</button>')
        + (OPT.hideAdditional ? '' : '<button type="button" class="btn btn-raised btn-info addtional-btn">{{additionalText}}</button>')
        + (OPT.hideDone ? '' : '<button type="button" class="btn btn-raised btn-primary done-btn">{{doneText}}</button>')
        + '</div>')

        + '</div>'
        + '</div>'
        + '</div>';

    // 删掉已经存在的modal
    if ($('.ocemodal').length > 0) {
        $('.ocemodal').remove();
        $('.modal-backdrop').remove();
    }

    // 插入
    $('body').append(temp(dom, OPT));

    if (OPT.url) {
        $('.ocemodal .oceModal-content').load(OPT.url);
    } else {
        if (OPT.$content) { // 优先使用$content
            $('.ocemodal .oceModal-content').append(OPT.$content);
        } else {
            $('.ocemodal .oceModal-content').html(OPT.content);
        }
    }

    $('.ocemodal').modal({
        keyboard : true,
        backdrop : OPT.static?'static':true
    });

    $('.ocemodal').on('show.bs.modal', OPT.show).modal('show').trigger('show');

    // $('.ocemodal')
    //     .on('shown.bs.modal', OPT.shown)
    //     .on('show.bs.modal', OPT.show)
    //     .on('hide.bs.modal', function(e) {
    //         OPT.hide(e);
    //     })
    //     .on('hidden.bs.modal', OPT.hidden)
    //     .on('loaded.bs.modal', OPT.loaded)
    //     .modal('show').trigger('shown');

    $('.ocemodal .done-btn').click(function(e) {
        OPT.doneFn(e);
    });
    $('.ocemodal .cancel-btn').click(function(e) {
        OPT.cancelFn(e);
    });
    $('.ocemodal .addtional-btn').click(function(e) {
        OPT.additionalFn(e);
    });
};
